package com.example.exercise.app.user.service.impl;

import com.example.exercise.app.user.dto.response.UserLoginInfo;
import com.example.exercise.app.user.mapper.UserMapper;
import com.example.exercise.app.user.mapper.entity.UserEntity;
import com.example.exercise.app.user.service.UserService;
import com.example.exercise.app.user.dto.request.UserRequest;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Random;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    //@Transactional(readOnly = true)
    @Override
    public UserLoginInfo login(String userName, String password) throws Exception {
        UserEntity userEntity = userMapper.login(userName, DigestUtils.md5DigestAsHex(password.getBytes()));
        if (userEntity != null) {
            Integer userId = userEntity.getUserId();
            UserLoginInfo userLoginInfo = new UserLoginInfo();
            userLoginInfo.setUserId(userId);
            userLoginInfo.setUserName(userName);
            userLoginInfo.setIfAdmin(userEntity.getIfAdmin());
            userLoginInfo.setUseState(userEntity.getUseState());
            userLoginInfo.setSex(userEntity.getSex());
            userLoginInfo.setPersonDesc(userEntity.getPersonDesc());
//            tokenManager.flushUserType(userId,userEntity.getIfAdmin());
            return userLoginInfo;
        }
        return null;
    }

    @Override
    public Integer register(UserRequest userRequest) {
        Integer userId = userMapper.findByUserName(userRequest.getUserName());
        if(userId != null){//用户名已注册
            return 1;
        }
        String password = DigestUtils.md5DigestAsHex(userRequest.getPassword().getBytes());
        UserEntity userEntity = new UserEntity();
        userEntity.setUserName(userRequest.getUserName());
        userEntity.setPassword(password);
        userEntity.setEmail(userRequest.getEmail());
        userEntity.setSex(userRequest.getSex());
        userEntity.setPersonDesc(userRequest.getPersonDesc());
        userEntity.setNickname(userRequest.getNickname());
        userMapper.register(userEntity);
        return 0;
    }

    private String generateRandCode() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            stringBuilder.append(new Random().nextInt(10));
        }
        return stringBuilder.toString();
    }

    @Override
    public int resetPassword(String verifyCode, String userName, String password) {
        Integer userId = userMapper.findByUserName(userName);
        if(userId == null) {
            return 1;
        }
        userMapper.updatePassword(userId, password);
        return 0;
    }

    @Override
    public int updatePersonEmail(String verifyCode, Integer userId, String email) {
        Integer userId1 = userMapper.findByEmail(email);
        if(userId1 != null){
            return 2;
        }
        userMapper.updateEmail(userId,email);
        return 0;
    }

    @Transactional(readOnly = true)
    @Override
    public UserEntity findByUserId(Integer userId) {
        UserEntity userEntity = userMapper.findByUserId(userId);
        userEntity.setPassword(null);
        return userEntity;
    }

    @Override
    public void updatePersonInfo(UserEntity userEntity) {
        userMapper.updatePersonInfo(userEntity);
    }

    @Override
    public boolean updatePassword(Integer userId, String password, String newPassword) {
        UserEntity userEntity0 = userMapper.findByUserId(userId);
        UserEntity userEntity = userMapper.login(userEntity0.getUserName(), DigestUtils.md5DigestAsHex(password.getBytes()));
        if(userEntity == null){
            return false;
        }
        userMapper.updatePassword(userId,DigestUtils.md5DigestAsHex(newPassword.getBytes()));
        return true;
    }

}
